package cn.edu.tjut.control;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import cn.edu.tjut.bean.ExpclassBean;
import cn.edu.tjut.bean.ReportBean;
import cn.edu.tjut.bean.StudentBean;
import cn.edu.tjut.myUtils.MyUtils;
import cn.edu.tjut.seriver.ExpclassService;
import cn.edu.tjut.seriver.StuService;
import cn.edu.tjut.seriver.impl.ReportInfoService;

@Controller
public class ReportInfoControl {
	// *************实验报告息管理**************************************//
	@Resource
	private ReportInfoService repService;
	@Resource
	private ExpclassService expService;
	@Resource
	private StuService stuService;

	// 教师根据学生班级，学号动态查询实验报告信息
	@RequestMapping(value = "/selectRepDynamic.do")
	public String selectRepDynamic(ReportBean repB, ExpclassBean expB, HttpSession session, HttpServletRequest request,
			HttpServletResponse response) {
		List<ReportBean> lstRep = repService.selectRepInfoDynamic(repB);
		// 需要重新获取一次实验课程的缓存
		ExpclassBean expB2 = new ExpclassBean();
		expB2 = expService.selectByexpIndex(MyUtils.Integer2String(expB.getExpIndex()));
		request.setAttribute("expB", expB2);// 设置缓存
		request.setAttribute("stucla", repB.getStuClass());// 设置班级缓存
		request.setAttribute("repB", repB);// 设置缓存
		if (lstRep != null) {
			session.setAttribute("lstRep", lstRep);
			return "reportManager/TLookRrport";// 跳转到教师查询学生实验报告界面
		} else {
			session.setAttribute("Prompt", "暂时没有学生信息");
			return "reportManager/TLookRrport";
		}
	}

	// 填写实验报告
	@RequestMapping(value = "/saveReport.do")
	public String insertTeaInfo1(ReportBean repB, StudentBean stuB, HttpServletRequest request, HttpSession session,
			Model model, @RequestParam("file") CommonsMultipartFile file) throws Exception {
		String fileName = file.getOriginalFilename();
		if (fileName != "" || fileName != null) {
			repB.setRepDoc("fileUploadRep/" + fileName);
		}
		String path = "D:\\lanqiaoJava\\lanqiaoworkspace\\gra_Project\\src\\main\\webapp\\fileUploadRep\\" + fileName;
		File newFile = new File(path);
		if (!newFile.exists()) {
			file.transferTo(newFile);
		}
		int i = repService.insertRepInfo(repB);
		if (i > 0) {
			Long stuId = stuB.getStuId();
			StudentBean stuB2 = stuService.selectBystuId(stuId);
			List<ExpclassBean> lstExp = expService.selectBystuClass(stuB2.getStuClass());
			if (lstExp != null) {
				session.setAttribute("lstExp", lstExp);
				request.setAttribute("stuB", stuB2);
				return "expclassManager/SMyExp";
			} else {
				session.setAttribute("Prompt", "暂时没有您的实验报告信息");
				return "expclassManager/SMyExp";
			}
		} else {
			session.setAttribute("Prompt", "添加实验报告失败！");
			return "reportManager/SExpOver";
		}
	}

	// 查看某实验报告
	@RequestMapping(value = "/lookRepInfo.do")
	public String lookRepInfo(ReportBean repB, HttpServletRequest request, HttpServletResponse response) {
		String repIndex = request.getParameter("ope");// 获取实验报告的主键
		String opa = request.getParameter("opa");
		String opb = request.getParameter("opb");
		repB = repService.selectByrepIndex(repIndex);
		request.setAttribute("repB", repB);// 设置缓存
		if (!opb.equals("3")) {
			request.setAttribute("opa", opa);
			return "reportManager/ExpReportUpdate";// 跳转修改页面
		} else {
			String stuClass = request.getParameter("stuClass");
			String stuId = request.getParameter("stuId");
			request.setAttribute("opb", opb);
			request.setAttribute("stuClass", stuClass);
			request.setAttribute("stuId", stuId);
			return "reportManager/ExpReportUpdate1";// 跳转修改页面
		}

	}

	// 修改实验报告
	@RequestMapping(value = "/updateRepInfo.do")
	public String updateExpInfo(ReportBean repB, HttpServletRequest request, HttpSession session, Model model,
			@RequestParam("file") CommonsMultipartFile file) throws Exception {
		String fileName = file.getOriginalFilename();
		if (fileName != "") {
			repB.setRepDoc("fileUploadRep/" + fileName);
			String path = "D:\\lanqiaoJava\\lanqiaoworkspace\\gra_Project\\src\\main\\webapp\\fileUploadRep\\"
					+ fileName;
			File newFile = new File(path);
			if (!newFile.exists()) {
				file.transferTo(newFile);
			}
		}
		int i = repService.updateByRepIndex(repB);
		// String opt = request.getParameter("opt");
		String opa = request.getParameter("opa");
		if (!opa.equals("2")) {
			String opb = request.getParameter("opb");
			if (opb.equals("3")) {
				String stuClass = request.getParameter("stuClass");
				String stuId = request.getParameter("stuId");
				Long expId = repB.getExpId();
				ExpclassBean expB = expService.selectByexpId(expId);
				ReportBean repB2 = new ReportBean();
				repB2.setExpClass(stuClass);
				repB2.setStuId(MyUtils.obj2Long(stuId));
				List<ReportBean> lstRep = repService.selectRepInfoDynamic(repB2);
				request.setAttribute("expB", expB);// 设置缓存
				request.setAttribute("stucla", stuClass);// 设置班级缓存
				request.setAttribute("repB", repB2);// 设置缓存
				if (lstRep != null) {
					session.setAttribute("lstRep", lstRep);
					return "reportManager/TLookRrport";// 跳转到教师查询学生实验报告界面
				} else {
					session.setAttribute("Prompt", "暂时没有学生信息");
					return "reportManager/TLookRrport";
				}
			} else {
				StudentBean stuB = stuService.selectBystuId(repB.getStuId());
				trueClass(repB);
				List<ExpclassBean> lstExp = expService.selectBystuClass(repB.getStuClass());
				if (lstExp != null) {
					session.setAttribute("lstExp", lstExp);
					request.setAttribute("stuB", stuB);
					return "expclassManager/SMyExp";
				} else {
					session.setAttribute("Prompt", "暂时没有您的实验课程信息");
					return "expclassManager/SMyExp";
				}
			}
		} else {
			if (i > 0) {
				List<ReportBean> lstRep = repService.selectAllreport();
				if (lstRep != null) {
					session.setAttribute("lstRep", lstRep);
					return "reportManager/SExpOver";// 跳转学生所有的实验报告界面
				} else {
					session.setAttribute("Prompt", "暂时没有您的实验报告信息");
					return "reportManager/SExpOver";
				}
			} else {
				session.setAttribute("Prompt", "修改实验报告失败！");
				return "reportManager/SExpOver";
			}
		}
	}

	// 根据学号查看实验报告
	@RequestMapping(value = "/selectRepBystuId.do")
	public String selectRepBystuId(ReportBean repB, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) {
		String stuid = request.getParameter("stuid");

		List<ReportBean> lstRep = repService.selectBystuId(MyUtils.obj2Long(stuid));
		if (lstRep != null) {
			session.setAttribute("lstRep", lstRep);
			return "reportManager/SExpOver";// 跳转学生所有的实验报告界面
		} else {
			session.setAttribute("Prompt", "暂时没有您的实验报告信息");
			return "reportManager/SExpOver";
		}

	}

	// 删除实验报告
	@RequestMapping(value = "/deleteRepInfo.do")
	public String deleteExpInfo(ReportBean repB, HttpServletRequest request, HttpServletResponse response,
			HttpSession session) {
		String repIndex = request.getParameter("ope");// 获取实验报告的主键
		repB.setRepIndex(MyUtils.obj2Integer(repIndex));
		repService.deleteByrepIndex(repB);
		List<ReportBean> lstRep = repService.selectAllreport();
		if (lstRep != null) {
			session.setAttribute("lstRep", lstRep);
			return "reportManager/SExpOver";// 跳转学生所有的实验报告界面
		} else {
			session.setAttribute("Prompt", "暂时没有您的实验报告信息");
			return "reportManager/SExpOver";
		}
	}

	// 转换学生的班级（代号转换为文字）
	public void trueClass(ReportBean repB) {
		if (repB.getStuClass().equals("电信1班")) {
			repB.setStuClass("10");
		} else if (repB.getStuClass().equals("电信2班")) {
			repB.setStuClass("11");
		} else if (repB.getStuClass().equals("电科1班")) {
			repB.setStuClass("12");
		} else if (repB.getStuClass().equals("电科2班")) {
			repB.setStuClass("13");
		} else if (repB.getStuClass().equals("电科3班")) {
			repB.setStuClass("14");
		} else if (repB.getStuClass().equals("信息1班")) {
			repB.setStuClass("15");
		} else if (repB.getStuClass().equals("信息2班")) {
			repB.setStuClass("16");
		} else if (repB.getStuClass().equals("光电1班")) {
			repB.setStuClass("17");
		} else if (repB.getStuClass().equals("光电2班")) {
			repB.setStuClass("18");
		} else if (repB.getStuClass().equals("集成1班")) {
			repB.setStuClass("19");
		} else if (repB.getStuClass().equals("集成2班")) {
			repB.setStuClass("20");
		}
	}

}
